﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.IO;
using System.Text;
using Lead2012.Business;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Repository;
using Lead2012.Utility;

namespace Lead2012.Controllers
{
    public class DashboardController : BaseController
    {
        // Create an instance of dashboard management
        DashboardManagementRepository repository = new DashboardManagementRepository();

        [Authorize(Roles = "Administrator,Registered")]
        public ActionResult Index()
        {
            DashboardViewModel model = new DashboardViewModel();
            model.PageType = "Dashboard";
            model.FromDate = DateTime.Now.AddDays(-7);
            model.ToDate = DateTime.Now;
            SetupCommon(model, string.Empty, string.Empty);
            repository.GetModel(ref model);

            // Expired
            if (DateTime.Now.Month > 4)
                return Redirect("/Account/Login");

            return View(model);
        }

        [Authorize(Roles = "Administrator,Registered")]
        public ActionResult PrintCalendarEvent(int userId, string startDate)
        {
            DashboardViewModel model = new DashboardViewModel();
            model.CalendarList = repository.GetListCalendarEvent(userId, startDate);

            return View(model);
        }

        // Get calendar events
        [HttpPost]
        public ActionResult GetCalendarEvent(int userId, string dateSelected)
        {
            try
            {
                DashboardViewModel model = new DashboardViewModel();

                string[] arr = dateSelected.Split('/');
                string currentDate = string.Format("{0}-{1}-{2}", arr[2], arr[1], arr[0]);
                model.CalendarList = repository.GetListCalendarEvent(userId, dateSelected);
                model.TimeSelected = string.Format("{0}, {1}", LeadTools.GetDayText(currentDate), DateTime.Parse(currentDate).ToString("dd-MM-yyyy"));

                return Json(model, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        
        // Refresh milestone metrics
        [HttpPost]
        public ActionResult MilestoneReport(int sourceId, int userId, bool checkRange, string range, string startDate, string endDate)
        {
            if(checkRange)
            {
                startDate = range.Split('|')[0];
                endDate = range.Split('|')[1];
            }

            string[] arrStart = startDate.Split('/');
            string[] arrEnd = endDate.Split('/');

            DateTime start = DateTime.Parse(string.Format("{0}-{1}-{2}", arrStart[2], arrStart[1], arrStart[0]));
            start = LeadTools.StartOfDay(start);

            DateTime end = DateTime.Parse(string.Format("{0}-{1}-{2}", arrEnd[2], arrEnd[1], arrEnd[0]));
            end = LeadTools.EndOfDay(end);

            DashboardViewModel model = new DashboardViewModel();
            model.MilestoneModel = repository.GetMilestoneReport(sourceId, userId, start, end);
            return Json(model.MilestoneModel, JsonRequestBehavior.AllowGet);
        }

        // Refresh lead flow
        [HttpPost]
        public ActionResult LeadFlow(bool IsAdmin, int userId)
        {
            try
            {
                DashboardViewModel model = new DashboardViewModel();
                model.LeadFlowModel = repository.GetLeadFlowStatistic(IsAdmin, userId);
                return Json(model.LeadFlowModel, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }

        }
    }
}
